import { Button, Card, Space, Tabs,message } from 'antd';
import { useState } from 'react';
import useForceUpdate from 'use-force-update';
import { min_app_fs, min_app_shell } from '@linksaas-minapp/api';
import YAML from 'yaml'
import { Config } from '@/components/config';
import GlobalCfgPanel from '@/components/GlobalCfgPanel';
import RulePanel from '@/components/RulePanel';
import { CommentsRuleList } from '@/components/rules/comments';
import { ComplexityRuleList } from '@/components/rules/complexity';
import { CoroutinesRuleList } from '@/components/rules/coroutines';
import { EmptyBlocksRuleList } from '@/components/rules/emptyBlocks';
import { ExceptionsRuleList } from '@/components/rules/exceptions';
import { NamingRuleList } from '@/components/rules/naming';
import { PerformanceRuleList } from '@/components/rules/performance';
import { PotentialBugsRuleList } from '@/components/rules/potentialBugs';
import { StyleRuleList } from '@/components/rules/style';
import { FormattingRuleList } from '@/components/rules/formatting';
import { LibrariesRuleList } from '@/components/rules/libraries';


export default function Home() {
  const forceUpdate = useForceUpdate();
  const [keteCfg, setKeteCfg] = useState<Config>({});

  const openFile = async () => {
    let files = await min_app_fs.readFile(
      false,
      "yaml文件", ["yml", "yaml"]
    );
    if (files.length == 0) {
      return;
    }
    try {
      const content = new TextDecoder("utf-8").decode(files[0].file_data);
      const config = YAML.parse(content) as unknown as Config;
      setKeteCfg(config);
      forceUpdate();
    } catch (e) {
      console.log(e);
      message.error("加载配置失败");
    }
  };

  const saveFile = async () => {
    const content = YAML.stringify(keteCfg);
    const data = new TextEncoder().encode(content);
    await min_app_fs.writeFile("config.yml", data);
  }


  return (
    <Card title="detekt配置助手" extra={
      <Space>
        <Button type="link" onClick={e => {
          e.stopPropagation();
          e.preventDefault();
          min_app_shell.openBrowser("https://detekt.dev/");
        }}>官方网站</Button>
        <Button onClick={e => {
          e.stopPropagation();
          e.preventDefault();
          setKeteCfg({});
          forceUpdate();
        }}>重置配置</Button>
        <Button onClick={e => {
          e.stopPropagation();
          e.preventDefault();
          openFile();
        }}>导入</Button>
        <Button onClick={e => {
          e.stopPropagation();
          e.preventDefault();
          saveFile();
        }}>导出</Button>
      </Space>
    }>
      <Tabs defaultActiveKey="global">
        <Tabs.TabPane key="global" tab="全局配置">
          <GlobalCfgPanel cfg={keteCfg} onChange={cfg => {
            setKeteCfg(cfg);
            forceUpdate();
          }} />
        </Tabs.TabPane>
        <Tabs.TabPane key="comments" tab="注释规则">
          <RulePanel ruleCfg={keteCfg["comments"] ?? {}} onChange={ruleCfg => {
            setKeteCfg({ ...keteCfg, "comments": ruleCfg });
            forceUpdate();
          }} ruleList={CommentsRuleList} />
        </Tabs.TabPane>
        <Tabs.TabPane key="complexity" tab="复杂度规则">
          <RulePanel ruleCfg={keteCfg["complexity"] ?? {}} onChange={ruleCfg => {
            setKeteCfg({ ...keteCfg, "complexity": ruleCfg });
            forceUpdate();
          }} ruleList={ComplexityRuleList} />
        </Tabs.TabPane>
        <Tabs.TabPane key="coroutines" tab="协程规则">
          <RulePanel ruleCfg={keteCfg["coroutines"] ?? {}} onChange={ruleCfg => {
            setKeteCfg({ ...keteCfg, "coroutines": ruleCfg });
            forceUpdate();
          }} ruleList={CoroutinesRuleList} />
        </Tabs.TabPane>
        <Tabs.TabPane key="empty-blocks" tab="空白块规则">
          <RulePanel ruleCfg={keteCfg["empty-blocks"] ?? {}} onChange={ruleCfg => {
            setKeteCfg({ ...keteCfg, "empty-blocks": ruleCfg });
            forceUpdate();
          }} ruleList={EmptyBlocksRuleList} />
        </Tabs.TabPane>
        <Tabs.TabPane key="exceptions" tab="异常规则">
          <RulePanel ruleCfg={keteCfg["exceptions"] ?? {}} onChange={ruleCfg => {
            setKeteCfg({ ...keteCfg, "exceptions": ruleCfg });
            forceUpdate();
          }} ruleList={ExceptionsRuleList} />
        </Tabs.TabPane>
        <Tabs.TabPane key="formatting" tab="布局风格规则">
          <RulePanel ruleCfg={keteCfg["formatting"] ?? {}} onChange={ruleCfg => {
            setKeteCfg({ ...keteCfg, "formatting": ruleCfg });
            forceUpdate();
          }} ruleList={FormattingRuleList} />
        </Tabs.TabPane>
        <Tabs.TabPane key="libraries" tab="库相关规则">
          <RulePanel ruleCfg={keteCfg["libraries"] ?? {}} onChange={ruleCfg => {
            setKeteCfg({ ...keteCfg, "libraries": ruleCfg });
            forceUpdate();
          }} ruleList={LibrariesRuleList} />
        </Tabs.TabPane>
        <Tabs.TabPane key="naming" tab="命名规则">
          <RulePanel ruleCfg={keteCfg["naming"] ?? {}} onChange={ruleCfg => {
            setKeteCfg({ ...keteCfg, "naming": ruleCfg });
            forceUpdate();
          }} ruleList={NamingRuleList} />
        </Tabs.TabPane>
        <Tabs.TabPane key="performance" tab="性能规则">
          <RulePanel ruleCfg={keteCfg["performance"] ?? {}} onChange={ruleCfg => {
            setKeteCfg({ ...keteCfg, "performance": ruleCfg });
            forceUpdate();
          }} ruleList={PerformanceRuleList} />
        </Tabs.TabPane>
        <Tabs.TabPane key="potential-bugs" tab="潜在缺陷规则">
          <RulePanel ruleCfg={keteCfg["potential-bugs"] ?? {}} onChange={ruleCfg => {
            setKeteCfg({ ...keteCfg, "potential-bugs": ruleCfg });
            forceUpdate();
          }} ruleList={PotentialBugsRuleList} />
        </Tabs.TabPane>
        <Tabs.TabPane key="style" tab="代码规则规则">
          <RulePanel ruleCfg={keteCfg["style"] ?? {}} onChange={ruleCfg => {
            setKeteCfg({ ...keteCfg, "style": ruleCfg });
            forceUpdate();
          }} ruleList={StyleRuleList} />
        </Tabs.TabPane>
      </Tabs>
    </Card>
  )
}
